package com.yupi.utils;

import cn.hutool.core.util.StrUtil;
import com.yupi.model.ExecutorMessage;
import org.springframework.util.StopWatch;

import java.io.*;

/**
 * @Author FengHuan Wang
 * @Date 2025/3/10 10:18
 * @Version 1.0
 */

/**
 * 进程工具类
 */
public class ProcessUtils {

    /**
     * 静态执行
     * @param process
     * @param opName
     * @return
     */
    public static ExecutorMessage runProcess(Process process, String opName) {
        ExecutorMessage executorMessage = new ExecutorMessage();
        try {
            //开始记录程序的执行耗时
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            //等待程序执行，获取错误码
            int execCode = process.waitFor();
            executorMessage.setExecCode(execCode);
            //正常退出
            if (execCode == 0) {
                System.out.println(opName + "成功");
                //分批获取进程的输出
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                StringBuilder compileOutputStringBuilder = new StringBuilder();
                //逐行读取
                String compileOutputLine;
                while ((compileOutputLine = bufferedReader.readLine()) != null) {
                    compileOutputStringBuilder.append(compileOutputLine);
                }
                executorMessage.setMessage(compileOutputStringBuilder.toString());
                System.out.println(compileOutputStringBuilder.toString());
            } else {
                //异常退出
                System.out.println(opName + "错误码 = " + execCode);

                //分批获取进程的输出
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                StringBuilder compileOutputStringBuilder = new StringBuilder();
                //逐行读取
                String compileOutputLine;
                while ((compileOutputLine = bufferedReader.readLine()) != null) {
                    compileOutputStringBuilder.append(compileOutputLine);
                }
                //分批获取程序的错误输出
                BufferedReader errorBufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                StringBuilder errorcompileOutputStringBuilder = new StringBuilder();
                //逐行读取
                String errorCompileOutputLine;
                while ((errorCompileOutputLine = errorBufferedReader.readLine()) != null) {
                    errorcompileOutputStringBuilder.append(errorCompileOutputLine);
                }
                System.out.println(errorcompileOutputStringBuilder.toString());
                executorMessage.setErrorMessage(errorcompileOutputStringBuilder.toString());
            }
            //对相关资源进行销毁，回收
            stopWatch.stop();
            executorMessage.setTime(stopWatch.getLastTaskTimeMillis());
            process.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return executorMessage;
    }


    /**
     * 动态执行
     * @param process
     * @param args
     * @return
     */
    public static ExecutorMessage runInterProcess(Process process, String args) {
        ExecutorMessage executorMessage = new ExecutorMessage();
        try {
            //向控制台输入参数
            InputStream inputStream = process.getInputStream();
            OutputStream outputStream = process.getOutputStream();

            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
            String[] s = args.split(" ");
            String join = StrUtil.join("\n", s) + "\n";
            outputStreamWriter.write(join);
            outputStreamWriter.flush();

            //分批获取进程的输出
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder compileOutputStringBuilder = new StringBuilder();
            //逐行读取
            String compileOutputLine;
            while ((compileOutputLine = bufferedReader.readLine()) != null) {
                compileOutputStringBuilder.append(compileOutputLine);
            }
            executorMessage.setMessage(compileOutputStringBuilder.toString());
            // 对相关资源进行回收
            outputStreamWriter.close();
            inputStream.close();
            outputStream.close();
            process.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return executorMessage;
    }
}